package com.atguigu.mapreduce.demo.outputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2021/11/30 18:41
 * @description:
 */
public class LogDriver {


    static String srcPath = "D:\\testarea\\bigdatatest\\hadoop\\mapreduce\\11_input\\inputoutputformat";
    static String dstPath = "D:\\testarea\\bigdatatest\\hadoop\\mapreduce\\11_input\\inputoutputformat\\out";


    public static void main(String[] args) {

        try {
            // job
            Configuration conf = new Configuration();
            Job job = Job.getInstance(conf);

            // jar path
            job.setJarByClass(LogDriver.class);

            // mapper  reducer  关联
            job.setMapperClass(LogMapper.class);
            job.setReducerClass(LogReducer.class);

            // map out k v
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(NullWritable.class);

            // reducer out k v 有的可能不存在
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(NullWritable.class);

            //outputFormat setting
            job.setOutputFormatClass(LogOutputFormat.class);


            FileInputFormat.setInputPaths(job, new Path(srcPath));
            //已经在outputFormat中指明了输出方式, 这里为何还要写呢??? 这里其实就是一个标记而已, _SUCCESS文件是空的
            FileOutputFormat.setOutputPath(job, new Path(dstPath));

            // 提交job
            boolean resFlag = job.waitForCompletion(true);
            System.exit(resFlag ? 0 : 1);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}

